package com.si.rapid.XML;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;



public class XMLHandler extends DefaultHandler {

	Boolean currentElement = false;
	String currentValue = null;
	StringBuffer buf=new StringBuffer();
	int flag=0;
	public static SearchList searchList = null;
	public static RapidList rapidList = null;
	

	public static SearchList getSitesList() {
		return searchList;
	}
	
	public static RapidList getRapidList() {
		return rapidList;
	}

	public static void setSitesList(SearchList searchList) {
		XMLHandler.searchList = searchList;
	}


	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		currentElement = true;

		if (localName.equals("answer"))
		{
			/** Start */
			searchList = new SearchList();
		}
		if (localName.equals("head"))
		{
			/** Start */
			rapidList = new RapidList();
		}
		if (localName.equals("details"))
		{
			//Open String Buffer to Concatenate
			buf=new StringBuffer();
			flag=1;
		}
		
//		} else if (localName.equals("name")) {
//			/** Get attribute value */
//			String attr = attributes.getValue("category");
//			searchList.setCategory(attr);
//		}

	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		

		/** set value */
		if (localName.equalsIgnoreCase("name"))
			searchList.setName(currentValue);

		else if (localName.equalsIgnoreCase("details")){
			searchList.setAddress(buf.toString());
			flag=0;
			//currentValue.trim();
		}		
		//In case analyzing rapidshare
		else if (localName.equalsIgnoreCase("pre"))
			rapidList.setAddress(currentValue);
		
		
		currentElement = false;
	}

	/** Called to get tag characters ( ex:- <name>AndroidPeople</name>
	 * -- to get AndroidPeople Character ) */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
			
			if(flag==1)
				buf.append(ch,start,length);
			currentValue = new String(ch, start, length);


	}

}
